/**
 * @Author:Geln Yang
 * @Created:2008-9-23 $ Revision: 1.0 $ $ Author: Geln Yang $ $ Date: 2008-9-23 $
 */
package com.hgy.commonsale.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;

import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;

/**
 * @Author:Geln Yang
 * @Created:2008-9-23
 */
public class DBSchemaExport {
	public static void main(String[] args) throws Exception {
		if (args.length < 3) {
			throw new IllegalArgumentException("hibernate.cfg file、hbm dir and toSQL dir is missing!");
		}

		File cfgFile = new File(args[0]);
		File baseDir = new File(args[1]);
		String schema_export_file = args[2];

		String patternFile = null;
		if (args.length == 4) {
			patternFile = args[3];
		}

		System.out.println("hibernate cfg file:" + cfgFile.getAbsolutePath());
		System.out.println("base class dir:" + baseDir.getAbsolutePath());
		System.out.println("export sql file:" + schema_export_file);
		Configuration cfg = new Configuration().configure(cfgFile);

		addFile(cfg, baseDir, ".+\\.hbm.xml");
		SchemaExport export = new SchemaExport(cfg);
		export.setOutputFile(schema_export_file);
		export.setDelimiter(";");
		export.create(true, true);
		if (patternFile != null && patternFile.trim().length() > 0) {
			change(schema_export_file, patternFile);
		}
	}

	private static void addFile(Configuration cfg, File dir, final String pattern) throws IOException {
		File[] files = FileUtils.listFiles(dir.getAbsolutePath(), pattern);
		for (int i = 0; i < files.length; i++) {
			cfg.addFile(files[i]);
			System.out.println(files[i].getAbsolutePath());
		}
		File[] subDirs = getSubDir(dir);
		if (subDirs != null && subDirs.length > 0) {
			for (int i = 0; i < subDirs.length; i++) {
				addFile(cfg, subDirs[i], pattern);
			}
		}
	}

	private static void change(String schema_export_file, String patternFile) throws Exception {
		final String content = Utils.getContent(schema_export_file, "UTF-8");
		final StringBuffer buffer = new StringBuffer();
		FileUtils.readBufferedTextFile(patternFile, "UTF-8", new ITextFileReader() {
			public void read(BufferedReader reader) throws IOException {
				String pattern = null;
				String destContent = content;
				while ((pattern = reader.readLine()) != null) {
					if (pattern.trim().length() == 0) {
						continue;
					}
					destContent = destContent.replaceAll(pattern, "$1nvarchar$2");
				}
				buffer.append(destContent);
			}
		});
		//System.out.println(buffer.toString());
		FileUtils.writeBufferedTextFile(schema_export_file, "UTF-8", new ITextFileWriter() {
			public void write(BufferedWriter writer) throws IOException {
				writer.write(buffer.toString());
			}
		});
	}

	private static File[] getSubDir(File dir) {
		return dir.listFiles(new FileFilter() {
			public boolean accept(File pathname) {
				return pathname.isDirectory();
			}

		});
	}
}
